package entrata;

import classi.utente;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Classe principale di welcome
 * 
 * @author sestari
 */
public class welcome extends HttpServlet {

    /**
     * Genera la pagina HTML per utente Medico
     * @param out
     * @param msg
     * @param sessione 
     */
    public void medicoHtml(PrintWriter out, String msg, HttpSession sessione) {


        
        //Fa la formattazione dell'ora e data
        Calendar calendar = new GregorianCalendar();
        DateFormat dataf = new SimpleDateFormat("dd'/'MM'/'yyyy");
        DateFormat oraf = new SimpleDateFormat("HH':'mm':'ss");
        String dataformattata = dataf.format(calendar.getTime());
        String oraformattata = oraf.format(calendar.getTime());


        String realPath = getServletContext().getRealPath("/pdf");
        utente medico = (utente) sessione.getAttribute("user");
        int idMedico = medico.getId();

        File f = new File(realPath, idMedico + ".pdf");



        out.println("<!DOCTYPE html> ");
        out.println("<html> ");
        out.println("    <head> ");
        out.println("        <title>welcome MEDICO</title> ");
        out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> ");
        out.println("    </head> ");
        out.println("    <body> ");
        out.println("        <div> ");
        out.println("<b>");
        out.println(msg);
        out.println("</b>");
        out.println("<br>");
        out.println("<br>");
        out.println("            <table border=\"1\" > ");
        out.println("                <tr>                 ");
        out.println("                <form action=\"richiamo\" method=\"post\"> ");



        out.println("                    <td> ");
        out.println("   Richiamo paziente da:  <input type=\"text\" name=\"richiamo_da\" value=\"1\"/>                         ");
        out.println("<br> <input type=\"radio\" name=\"tempo\" value=\"secondi\" checked> Secondi  ");
        out.println("<input type=\"radio\" name=\"tempo\" value=\"giorni\"> Giorni");
        out.println("<input type=\"radio\" name=\"tempo\" value=\"mesi\"> Mesi");
        out.println("<input type=\"radio\" name=\"tempo\" value=\"anni\"> Anni");

        out.println("                    </td> ");
        out.println("                    <td> ");
        out.println("                        <input type=\"submit\"  value=\"Richiamare\"/>        ");

        out.println("                    </td>                   ");
        out.println("                </form> ");




        out.println("                </tr> ");
        out.println("                 ");
        out.println("                <tr>                 ");
        out.println("                <form action=\"listaPaziente\" method=\"post\"> ");
        out.println("                    <td> ");
        out.println("Creare una lista dei pazienti dal: <br> Data <input type=\"text\" name=\"data\" value=\"" + dataformattata + "\"/>                         ");
        out.println("Ora:  <input type=\"text\" name=\"ora\" value=\"" + oraformattata + "\"/>                         ");
        out.println("                    </td> ");
        out.println("                    <td> ");
        out.println("                        <input type=\"submit\"  value=\"Genera Lista\"/>                         ");
        out.println("                    </td>                   ");
        out.println("                </form> ");
        out.println("                </tr> ");
        out.println("                <tr>                 ");
        out.println("                <form action=\"logoff\" method=\"post\"> ");
        out.println("                    <td> ");
        out.println("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
        out.println("                    </td>                   ");
        out.println("                </form> ");
        out.println("                    <td> ");
        out.println("                    </td> ");
        out.println("                </tr> ");
        out.println("                 ");
        out.println("                 ");
        out.println("                 ");
        out.println("            </table> ");
        out.println(" ");
        out.println("          ");
        out.println("        </div> ");

        if (f.exists()) {
            out.println("<br>  Caro signor:<b> " + medico.getNome() + " " + medico.getCognome() + "</b>, il PDF delle lettere dei pazienti esiste: ");
            out.println("<A HREF=\"pdf/" + idMedico + ".pdf" + "\">Scarica PDF</A>");




        };

        out.println("    </body> ");
        out.println("</html> ");



    }

    /**
     * Genera la pagina HTML per utente Paziente
     * @param out
     * @param msg 
     */
    public void pazienteHtml(PrintWriter out, String msg) {

        out.println("<!DOCTYPE html> ");
        out.println("<html> ");
        out.println("    <head> ");
        out.println("        <title>welcome PAZIENTE</title> ");
        out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> ");
        out.println("    </head> ");
        out.println("    <body> ");
        out.println("        <div> ");
        out.println("<b>");
        out.println(msg);
        out.println("</b>");
        out.println("<br>");
        out.println("<br>");
        out.println("            <table border=\"1\" > ");
        out.println("                <tr>                 ");
        out.println("                <form action=\"listaVaccinazione\" method=\"post\"> ");
        out.println("                    <td> ");
        out.println(" Vissualizare vaccinazione:                         ");
        out.println("                    </td> ");
        out.println("                    <td> ");
        out.println("                        <input type=\"submit\"  value=\"Visualizza\"/>                         ");
        out.println("                    </td>                   ");
        out.println("                </form> ");
        out.println("                </tr> ");
        out.println("                 ");

        out.println("                <tr>                 ");
        out.println("                <form action=\"logoff\" method=\"post\"> ");
        out.println("                    <td> ");
        out.println("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
        out.println("                    </td>                   ");
        out.println("                </form> ");
        out.println("                    <td>                   ");
        out.println("                    </td>                   ");
        out.println("                </tr> ");
        out.println("                 ");
        out.println("                 ");
        out.println("                 ");
        out.println("            </table> ");
        out.println(" ");
        out.println("          ");
        out.println("        </div> ");
        out.println("    </body> ");
        out.println("</html> ");

    }

    /**
     * Verifica il tipo di utente (Medico/Paziente) e crea una pagina diversa a seconda del tipo di utente
     * controlla eventuali messagi
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        utente user = (utente) session.getAttribute("user");

        String msg = "";
        try {

            if (user.isMedico()) {

                if (request.getParameter("msge") != null) {
                    msg = request.getParameter("msge");
                }
                msg = msg + "<br>" + session.getAttribute("Ultimoacesso") + "<br>";
                medicoHtml(out, msg, session);

            } else {
                msg = "<br>" + session.getAttribute("Ultimoacesso") + "<br>";
                pazienteHtml(out, msg);
            }
        } finally {
            out.close();
        }


    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
